package com.zoo.diannaopeijian.controller.component;

import com.github.pagehelper.PageInfo;
import com.zoo.diannaopeijian.comm.page.Page;
import com.zoo.diannaopeijian.comm.tree.TreeUtil;
import com.zoo.diannaopeijian.controller.BaseController;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.domain.WareParameterDetail;
import com.zoo.diannaopeijian.service.WareComponentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-19.
 */
@RestController
@RequestMapping("/WareComponent")
public class WareComponentController extends BaseController {
    @Resource
    private WareComponentService wareComponentService;

    @RequestMapping("/add")
    public String addWareComponent(WareParameterDetail wareParameterDetail) {
        return wareComponentService.addWareComponent(wareParameterDetail) ? success : fail;
    }

    @RequestMapping("/delete")
    public String removeWareComponent(WareParameterDetail wareParameterDetail) {
        return wareComponentService.removeWareComponent(wareParameterDetail) ? success : fail;
    }

    @RequestMapping("/get")
    public List<Map> getWareComponent(Integer wareid) {
        List<String> nodeName = new ArrayList<>();
        nodeName.add("component");
        nodeName.add("parameter");
        nodeName.add("detail");

        return TreeUtil.buildEasyUITree(wareComponentService.getWareComponent(wareid), nodeName);
    }

    @RequestMapping("/getComponentWare")
    public Map<String, Object> getComponentWare(Page page,
                                       @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
                                       Integer componentid,
                                       Integer componentparameterdetailid,
                                       Ware ware) {
        Map<String, Object> result = new HashMap<>();

        List<Ware> datas = isFuzzy ? wareComponentService.getFuzzyComponentWareWithPage(ware, page, componentid, componentparameterdetailid) : wareComponentService.getComponentWareWithPage(ware, page, componentid, componentparameterdetailid);

        result.put(rowsWord, datas);
        result.put(totalWord, new PageInfo<Ware>(datas).getTotal());

        return result;
    }
}
